package com.ftw.commons.language

/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019-06-19  19:07
 */

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.util.Log
import com.ftw.commons.R
import java.util.*

object LocalManageUtil {

    private const val TAG = "LocalManageUtil"

    /**
     * 获取系统的locale
     *
     * @return Locale对象
     */
    private fun getSystemLocale(context: Context): Locale =
        LocalUtils.getInstance(context)!!.systemCurrentLocal

    fun getSelectLanguage(context: Context): String {
        return when (LocalUtils.getInstance(context)!!.selectLanguage) {
            0 -> context.getString(R.string.language_auto)
            1 -> context.getString(R.string.language_cn)
            2 -> context.getString(R.string.language_traditional)
            3 -> context.getString(R.string.language_en)
            else -> context.getString(R.string.language_en)
        }
    }

    /**
     * 获取选择的语言设置
     *
     * @param context
     * @return
     */
    private fun getSetLanguageLocale(context: Context): Locale {
        return when (LocalUtils.getInstance(context)!!.selectLanguage) {
            0 -> getSystemLocale(context)
            1 -> Locale.CHINA
            2 -> Locale.TAIWAN
            3 -> Locale.ENGLISH
            else -> Locale.ENGLISH
        }
    }

    fun saveSelectLanguage(context: Context, select: Int) {
        LocalUtils.getInstance(context)!!.saveLanguage(select)
        setApplicationLanguage(context)
    }

    fun setLocal(context: Context): Context {
        return updateResources(
            context,
            getSetLanguageLocale(context)
        )
    }

    @SuppressLint("ObsoleteSdkInt")
    private fun updateResources(context: Context, locale: Locale): Context {
        var contextVal = context

        Locale.setDefault(locale)

        val res = contextVal.resources
        val config = Configuration(res.configuration)
        if (Build.VERSION.SDK_INT >= 17) {
            config.setLocale(locale)
            contextVal = context.createConfigurationContext(config)
        } else {
            config.locale = locale
            res.updateConfiguration(config, res.displayMetrics)
        }
        return contextVal
    }

    /**
     * 设置语言类型
     */
    fun setApplicationLanguage(context: Context) {
        val resources = context.applicationContext.resources
        val dm = resources.displayMetrics
        val config = resources.configuration
        val locale = getSetLanguageLocale(context)
        config.locale = locale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val localeList = LocaleList(locale)
            LocaleList.setDefault(localeList)
            config.locales = localeList
            context.applicationContext.createConfigurationContext(config)
            Locale.setDefault(locale)
        }
        resources.updateConfiguration(config, dm)
    }

    fun saveSystemCurrentLanguage(context: Context) {
        val locale: Locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList.getDefault().get(0)
        } else {
            Locale.getDefault()
        }
        Log.d(TAG, locale.language)
        LocalUtils.getInstance(context)!!.systemCurrentLocal = locale
    }

    fun onConfigurationChanged(context: Context) {
        saveSystemCurrentLanguage(context)
        setLocal(context)
        setApplicationLanguage(context)
    }
}